﻿using System;
using System.Collections.Generic;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Platform;
using System.Device.Location;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Helpers;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using TestApp.Model;
using TestApp.Model.Data;
using TestApp.ViewModel.ItemsViewModel;
using TestApp.Model.Managers;
using TestApp.Navigation;
using TestApp.Messages;
using TestApp.WebService;

namespace TestApp.ViewModel.PagesViewModels
{
    public class StoresViewModel : ViewModelBase
    {
        // Property Names
        private static String StoresProperty = "Stores";
        private static String SearchBarIsFocusedProperty = "SearchBarIsFocused";
        private static String PushPinStoresProperty = "PushPinStores";
        private static String MapCenterProperty = "StoresRect";
        private static String SelectedIndexProperty = "SelectedIndex";

        // Private members
        private List<StoreItemViewModel> _stores;
        private List<Pushpin> _pushPinStores;
        private LocationRect _storesRect;
        private Boolean _isFocused;
        
        // Stores
        public List<StoreItemViewModel> Stores
        {
            get
            {
                return _stores;
            }
            set
            {
                _stores = value;
                RaisePropertyChanged(StoresProperty);
            }
        }

        // PushPins Stores
        public List<Pushpin> PushPinStores
        {
            get
            {
                return _pushPinStores;
            }
            set
            {
                _pushPinStores = value;
                RaisePropertyChanged(PushPinStoresProperty);
            }
        }

        // Map Center
        public LocationRect StoresRect
        {
            get
            {
                return _storesRect;
            }
            set
            {
                _storesRect = value;
                RaisePropertyChanged(MapCenterProperty);
            }
        }

        // Enter Button Pressed
        public RelayCommand<String> OnEnterPressed
        {
            get;
            private set;
        }

        // Location Button Pressed
        public RelayCommand OnLocationPressed
        {
            get;
            set;
        }

        // OnPageLoaded
        public RelayCommand OnPageLoaded
        {
            get;
            set;
        }

        // Selection Changed
        public RelayCommand<Int32> OnSelectionChanged
        {
            get;
            set;
        }

        // Is Focused
        public Boolean SearchBarIsFocused
        {
            get
            {
                return _isFocused;
            }
            set
            {
                _isFocused = value;
                RaisePropertyChanged(SearchBarIsFocusedProperty);
            }
        }

        // Selected Item
        public Int32 SelectedIndex
        {
            get
            {
                return -1;
            }
        }

        // Navigation Service
        private INavigationService NavigationService;

        // Constructor
        public StoresViewModel(INavigationService navigationService)
        {
            OnEnterPressed = new RelayCommand<String>((c) => EnterPressed(c));
            OnLocationPressed = new RelayCommand(LocationPressed);
            OnSelectionChanged = new RelayCommand<Int32>((c) => SelectionChanged(c));
            OnPageLoaded = new RelayCommand(PageLoaded);
            NavigationService = navigationService;

        }

        // Enter Pressed
        private async void EnterPressed(String searchText)
        {
            // Set IsFocused
            SearchBarIsFocused = false;
            try
            {
                // Search
                await StoresManager.Instance.SearchStoresByZip(searchText);

                // Initialize stores and push pins
                InitStoresAndPushPins();
            }
            catch (WSException e)
            {
                Console.WriteLine(e.Message);
            }
        }

        // Location Pressed
        private void LocationPressed()
        {
            SearchBarIsFocused = true;
        }

        // Selection Changed
        private void SelectionChanged(Int32 selectedIndex)
        {
            RaisePropertyChanged(SelectedIndexProperty);
        }

        // Page Loaded
        private void PageLoaded()
        {
            InitStoresAndPushPins();
        }


        // Init Stores and Push Pins
        private void InitStoresAndPushPins()
        {
            List<Store> stores = StoresManager.Instance.Stores;

            if (stores != null)
            {

                _stores = new List<StoreItemViewModel>();
                _pushPinStores = new List<Pushpin>();

                Double minLatitude = Double.MaxValue;
                Double maxLatitude = Double.MinValue;
                Double minLongitude = Double.MaxValue;
                Double maxLongitude = Double.MinValue;

                foreach (Store store in stores)
                {
                    _stores.Add(new StoreItemViewModel(store));

                    // New Push Pin
                    Pushpin storePushPin = new Pushpin();
                    Location location = new Location();
                    location.Latitude = store.Latitude;
                    location.Longitude = store.Longitude;
                    storePushPin.Content = store.Name;
                    storePushPin.Location = location;
                    _pushPinStores.Add(storePushPin);

                    minLatitude = minLatitude > store.Latitude ? store.Latitude : minLatitude;
                    maxLatitude = maxLatitude < store.Latitude ? store.Latitude : maxLatitude;
                    minLongitude = minLongitude > store.Longitude ? store.Longitude : minLongitude;
                    maxLongitude = maxLongitude < store.Longitude ? store.Longitude : maxLongitude;
                }
                Stores = _stores;
                PushPinStores = _pushPinStores;

                // Set Location Rect
                StoresRect = new LocationRect(maxLatitude, minLongitude, minLatitude, maxLongitude);
            }
        }
    }
}
